package com.ruanyun.modular.admin.service.impl;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.consts.PublicCommon;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.modular.admin.entity.TPollutantsFactor;
import com.ruanyun.modular.admin.mapper.TCommitReportMapper;
import com.ruanyun.modular.admin.service.VocReducingPlanService;
import com.ruanyun.sys.core.shiro.ShiroKit;
import com.ruanyun.modular.admin.mapper.TPollutantsFactorMapper;
import com.ruanyun.modular.admin.model.params.TPollutantsFactorParam;
import com.ruanyun.modular.admin.model.result.TPollutantsFactorResult;
import  com.ruanyun.modular.admin.service.TPollutantsFactorService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruanyun.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 污染物信息表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-05-12
 */
@Service
public class TPollutantsFactorServiceImpl extends ServiceImpl<TPollutantsFactorMapper, TPollutantsFactor> implements TPollutantsFactorService {

    @Autowired
    private UserService userService;
    @Autowired
    private TCommitReportMapper commitReportMapper;

    @Override
    public ResponseData add(TPollutantsFactorParam param){
        TPollutantsFactor entity = getEntity(param);
        entity.setDelFlag(PublicCommon.DEL_FLAG_NORMAL);
        Integer count = commitReportMapper.getIsCommit(userService.getCid());
        if(count > 0 ){
            return  ResponseData.error("该条信息已提交报告无法继续添加");
        }

        int result = this.baseMapper.insert(entity);
        if(result == 0){
            return ResponseData.error("添加失败");
        }
        return ResponseData.success();
    }

    @Override
    public void delete(TPollutantsFactorParam param){
        this.removeById(getKey(param));
    }

    /**
    * 删除
    *
    * @param id
    * @return
    */
    @Override
    public ResponseData updateDelFlagById(Long id) {
        TPollutantsFactor entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该污染物信息表不存在");
        }
        entity.setDelFlag(PublicCommon.DEL_FLAG_DELETE);
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("删除失败");
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData update(TPollutantsFactorParam param){
        TPollutantsFactor oldEntity = getOldEntity(param);
        TPollutantsFactor newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        Integer count = commitReportMapper.getIsCommit(userService.getCid());
        if(count > 0 ){
            return  ResponseData.error("该条信息已提交报告无法修改");
        }
        int result = this.baseMapper.updateById(newEntity);
        if(result == 0){
        return ResponseData.error("修改失败失败");
        }
        return ResponseData.success();
    }

    @Override
    public TPollutantsFactorResult findBySpec(TPollutantsFactorParam param){
        return null;
    }

    @Override
    public List<TPollutantsFactorResult> findListBySpec(TPollutantsFactorParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(Integer monitoringId,Integer isOrganization ){
        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
        param.setMonitoringId(monitoringId);
        param.setIsOrganization(isOrganization);
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TPollutantsFactorParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TPollutantsFactor getOldEntity(TPollutantsFactorParam param) {
        return this.getById(getKey(param));
    }

    private TPollutantsFactor getEntity(TPollutantsFactorParam param) {
        TPollutantsFactor entity = new TPollutantsFactor();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    /**
        * 启用
        *
        * @param id
        * @return
        */
    @Override
    public ResponseData enableById(Long id) {
        TPollutantsFactor entity = this.baseMapper.selectById(id);
        if (entity == null) {
        return ResponseData.error("该污染物信息表不存在");
        }
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("启用失败");
        }
        return ResponseData.success();
    }

    /**
     * 禁用
     *
     * @param id
     * @return
     */
    @Override
    public ResponseData disableById(Long id) {
        TPollutantsFactor entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该污染物信息表不存在");
        }
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("禁用失败");
        }
        return ResponseData.success();
    }

    @Override
    public LayuiPageInfo findPageBySpecs(Integer monitoringId, Integer isOrganization, Integer isInside) {

        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
         param.setMonitoringId(monitoringId);
         param.setIsOrganization(isOrganization);
         param.setIsInside(isInside);
        IPage page = this.baseMapper.customPageList1(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo pollutantsList(Integer monitoringId, Integer isOrganization, Integer isInside,Integer pid) {



        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
        param.setMonitoringId(monitoringId);
        param.setIsOrganization(isOrganization);
        param.setIsInside(isInside);
        param.setPid(pid);
        IPage page = this.baseMapper.pollutantsList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }


    @Override
    public LayuiPageInfo pollutantsLists(Integer monitoringId, Integer isOrganization, Integer isInside,Integer windType) {



        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
        param.setMonitoringId(monitoringId);
        param.setIsOrganization(isOrganization);
        param.setIsInside(isInside);
        param.setWindType(windType);
        IPage page = this.baseMapper.pollutantsList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Page<TPollutantsFactorResult> getOrganizedInfo(Integer monitoringId, Integer isOrganization) {
        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
         param.setMonitoringId(monitoringId);
         param.setIsOrganization(isOrganization);
        Page<TPollutantsFactorResult> resultPage =  baseMapper.getOrganizedInfo(pageContext,param);

        return resultPage;
    }

    @Override
    public Page<TPollutantsFactorResult> getOrganizedPollutantsInfo(Integer monitoringId, Integer isOrganization, Integer orificeId) {
        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
        param.setMonitoringId(monitoringId);
        param.setIsOrganization(isOrganization);
        param.setOrificeId(orificeId);
        Page<TPollutantsFactorResult> resultPage =  baseMapper.getOrganizedPollutantsInfo(pageContext,param);
        return resultPage;
    }

    @Override
    public Page<TPollutantsFactorResult> getUnorganizedLocationInfo(Integer monitoringId, Integer isOrganization) {
        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
        param.setMonitoringId(monitoringId);
        param.setIsOrganization(isOrganization);
        Page<TPollutantsFactorResult> resultPage =  baseMapper.getUnorganizedLocationInfo(pageContext,param);

        return resultPage;
    }

    @Override
    public Page<TPollutantsFactorResult> getInsidePollutantsInfo(Integer monitoringId, Integer isOrganization,Integer id) {
        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
        param.setMonitoringId(monitoringId);
        param.setIsOrganization(isOrganization);
        param.setPid(id);
        Page<TPollutantsFactorResult> resultPage =  baseMapper.getInsidePollutantsInfo(pageContext,param);

        return resultPage;

    }

    @Override
    public Page<TPollutantsFactorResult> getUnInsidePollutantsInfo(Integer monitoringId, Integer isOrganization, Integer windType) {
        Page pageContext = getPageContext();
        TPollutantsFactorParam param = new TPollutantsFactorParam();
        param.setMonitoringId(monitoringId);
        param.setIsOrganization(isOrganization);
        param.setWindType(windType);
        Page<TPollutantsFactorResult> resultPage =  baseMapper.getUnInsidePollutantsInfo(pageContext,param);

        return resultPage;
    }

    @Override
    public List<TPollutantsFactorResult> getOrganizedList(Integer companyId) {
        return baseMapper.getOrganizedList(companyId);
    }

    @Override
    public List<TPollutantsFactorResult> getUnOrganizedList(Integer companyId, Integer windType) {
        return baseMapper.getUnOrganizedList(companyId,windType);
    }

    @Override
    public List<TPollutantsFactorResult> getInsidePositionList(Integer companyId) {
        return baseMapper.getInsidePositionList( companyId);
    }


}
